﻿Imports DTO
Imports System.Data.SqlClient
Public Class MONANDAO
    Private ketnoi As New DAO.connect()
    Public Function loadMONAN() As DataTable
        ketnoi.OpenConnection()
        Dim dtnv As New DataTable()
        Dim chuoi As String = "select * from MONAN"
        dtnv = ketnoi.getDataTable(chuoi)
        ketnoi.CloseConnection()
        Return dtnv
    End Function
    Private Function Sqlcommand_THEM(ByVal MAMONAN As Integer, ByVal TENMONAN As String, ByVal DONGIA As Double, ByVal LOAIMAMONAN As String) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("STOR_THEM_MONAN", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        'sqlcmd.Parameters.Add("@mode", SqlDbType.Int).Value = mode;
        sqlcmd.Parameters.Add("@MAMONAN", SqlDbType.Int).Value = MAMONAN
        sqlcmd.Parameters.Add("@TENMONAN", SqlDbType.NVarChar).Value = TENMONAN
        sqlcmd.Parameters.Add("@DONGIA", SqlDbType.Float).Value = DONGIA
        sqlcmd.Parameters.Add("@LOAIMAMONAN", SqlDbType.NVarChar).Value = LOAIMAMONAN


        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function them_MONAN(ByVal dto As MONANDTO)
        ketnoi.OpenConnection()
        'Dim dto As New LOAISANHDTO()
        If Sqlcommand_THEM(dto.MAMONAN, dto.TENMONAN, dto.DONGIA, dto.LOAIMAMONAN) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False
    End Function

    Private Function Sqlcommand_capnhat(ByVal MAMONAN As Integer, ByVal TENMONAN As String, ByVal DONGIA As Double, ByVal LOAIMAMONAN As String) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_CAPNHAT_MONAN", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        'sqlcmd.Parameters.Add("@mode", SqlDbType.Int).Value = mode;
        sqlcmd.Parameters.Add("@MAMONAN", SqlDbType.Int).Value = MAMONAN
        sqlcmd.Parameters.Add("@TENMONAN", SqlDbType.NVarChar).Value = TENMONAN
        sqlcmd.Parameters.Add("@DONGIA", SqlDbType.Float).Value = DONGIA
        sqlcmd.Parameters.Add("@LOAIMAMONAN", SqlDbType.NVarChar).Value = LOAIMAMONAN
        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function capnhat_MONAN(ByVal dto As MONANDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_capnhat(dto.MAMONAN, dto.TENMONAN, dto.DONGIA, dto.LOAIMAMONAN) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function

    Private Function Sqlcommand_XOA(ByVal MAMONAN As Integer) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_XOA_MONAN", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@MAMONAN", SqlDbType.Int).Value = MAMONAN

        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function xoa_MONAN(ByVal dto As MONANDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_XOA(dto.MAMONAN) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function

End Class
